package com.dietmanager.datalayer;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class DataLayer <E> {
		public final String LOG_FILE = "dailyLog.dm";
		public final String FOOD_FILE = "food.dm";
		private final String FILE_NAME;
		
		public DataLayer(String fileName) {
			FILE_NAME = fileName;
		}
		
		@SuppressWarnings("unchecked")
		public E readFile() {
			E object = null;
			try {
				FileInputStream fileIn = new FileInputStream(FILE_NAME);
				ObjectInputStream objectIn = new ObjectInputStream(fileIn);
				
				object = (E) objectIn.readObject();
				objectIn.close();

			} catch (Exception ex) {
				ex.printStackTrace();
			}
			
			return object;
		}

		public void writeFile(E object) {
			try {
				FileOutputStream fileOut = new FileOutputStream(FILE_NAME);
				ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);

				objectOut.writeObject(object);
				objectOut.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
}
